<template>
  <div style="height: 100%; width: 100%;">
    <div id="logo">
      <div id="app" style="height: 3%;">当前时间: {{timer | formatTimer}}</div>
      <h1>图书城</h1>
    </div>
  </div>
</template>

<script>
export default {
  name: "logo",
  data() {
    return {
      timer: new Date()
    };
  },
  mounted() {
    let _this = this; // 声明一个变量指向Vue实例this，保证作用域一致
    this.timer = setInterval(() => {
      _this.timer = new Date(); // 修改数据date
    }, 1000);
  },
  filters: {
    formatTimer: function(value) {
      let date = new Date(value);
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? "0" + MM : MM;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      let h = date.getHours();
      h = h < 10 ? "0" + h : h;
      let m = date.getMinutes();
      m = m < 10 ? "0" + m : m;
      let s = date.getSeconds();
      s = s < 10 ? "0" + s : s;
      return y + "-" + MM + "-" + d + " " + h + ":" + m;
    }
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer); // 在Vue实例销毁前，清除我们的定时器
    }
  }
};
</script>

<style>
#logo {
  background-size: 100% 100%;
  height: 100%;
}
</style>
